package com.jiang.admin.core.config;

import org.quartz.Scheduler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import com.jiang.admin.core.schedule.JobDynamicScheduler;

import javax.sql.DataSource;

/**
 * 调度器配置
 * @author jiangyougan 2018年12月2日 下午6:19:31 
 *
 */
@Configuration
public class JobDynamicSchedulerConfig {

    @Bean
    public SchedulerFactoryBean getSchedulerFactoryBean(DataSource dataSource){
        SchedulerFactoryBean schedulerFactory = new SchedulerFactoryBean();
        schedulerFactory.setDataSource(dataSource);
        schedulerFactory.setAutoStartup(true);                  // 自动启动
        schedulerFactory.setStartupDelay(20);                   // 延时启动，应用启动成功后在启动
        schedulerFactory.setOverwriteExistingJobs(true);        // 覆盖DB中JOB：true、以数据库中已经存在的为准：false
        schedulerFactory.setApplicationContextSchedulerContextKey("applicationContext");
        schedulerFactory.setConfigLocation(new ClassPathResource("quartz.properties"));
        return schedulerFactory;
    }

    @Bean
    public JobDynamicScheduler getJobDynamicScheduler(SchedulerFactoryBean schedulerFactory){
        Scheduler scheduler = schedulerFactory.getScheduler();
        JobDynamicScheduler jobDynamicScheduler = new JobDynamicScheduler();
        jobDynamicScheduler.setScheduler(scheduler);
        return jobDynamicScheduler;
    }

}
